package com.example.user.customviewdemo.slideletelistview.scroller;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.user.customviewdemo.R;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyScrollerListViewActivity extends AppCompatActivity {

    @Bind(R.id.my_scroller_listview)
    MyScrollerListView mListView;

    private static MyScrollerLinearLayout preScrollerLinearLayout;
    private MyScrollerListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_item_scroller_del);
        ButterKnife.bind(this);

        adapter = new MyScrollerListViewAdapter(this);
        mListView.setAdapter(adapter);
        adapter.setData(getData());

       /* mListView.setOnItemScrollListener(new MyScrollerListView.OnItemScrollListener() {
            @Override
            public void onScroll(MyScrollerLinearLayout myScrollerLinearLayout) {
                if(preScrollerLinearLayout != null){
                    preScrollerLinearLayout.smoothScrollTo(0,0);
                }
                preScrollerLinearLayout = myScrollerLinearLayout;
            }
        });*/

    }
    private ArrayList<MyScrollerListViewAdapter.DataHolder> getData(){
        ArrayList<MyScrollerListViewAdapter.DataHolder> data = new ArrayList<>();
        for (int i = 0;i<100;i++){
            MyScrollerListViewAdapter.DataHolder dataHolder = new MyScrollerListViewAdapter.DataHolder();
            dataHolder.title = "第"+i+"项";
            dataHolder.index = i;
            data.add(dataHolder);
        }
        return data;
    }



     static class MyScrollerListViewAdapter extends BaseAdapter {
        private ArrayList<DataHolder> data = new ArrayList<>();

        private LayoutInflater inflater;

        public MyScrollerListViewAdapter(Context context) {
            inflater = LayoutInflater.from(context);

        }

        public void setData(ArrayList<DataHolder> data) {
            this.data = data;
            notifyDataSetChanged();
        }

        /**
         * 删除item项的数据
         * @param position
         */
        public void removeItem(int position){
            if(position >= 0 && data.size() > position){
                data.remove(position);
                notifyDataSetChanged();
            }
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView == null){
                convertView = inflater.inflate(R.layout.item_scroll_listview_layout, null);
                viewHolder = new ViewHolder();
                viewHolder.itemContentView = (TextView) convertView.findViewById(R.id.item_content_view);
                viewHolder.itemDelView = (TextView) convertView.findViewById(R.id.item_del_view);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            //将数据加绑定到item上
            DataHolder dataHolder = data.get(position);
            viewHolder.itemContentView.setText(dataHolder.title);

            //给dataHolder中的rootLayout赋值，为了在listview的onTouchEvent中得到当前点击的rootLayout
            dataHolder.rootLayout = (MyScrollerLinearLayout) convertView;
            dataHolder.rootLayout.setOnScrollListener(new MyScrollerLinearLayout.OnScrollListener() {
                @Override
                public void onScroll(MyScrollerLinearLayout layout) {
                    if(preScrollerLinearLayout != null && preScrollerLinearLayout != layout){
                        preScrollerLinearLayout.smoothScrollTo(0,0);
                    }
                    preScrollerLinearLayout = layout;
                }
            });

            //因为我们的ListView的item会复用，所以需要我们将scroll归零
            dataHolder.rootLayout.scrollTo(0,0);

            //给删除按钮加上点击删除监听
            viewHolder.itemDelView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                    removeItem(position);
               }
           });

            return convertView;

        }


        static   class ViewHolder {
            TextView itemContentView;
            TextView itemDelView;
        }
        static   class DataHolder{
            public int index;
            public String title;
            public MyScrollerLinearLayout rootLayout;
        }
    }
}
